home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / X-Ray / X-Ray INIT Source / main.c < prev    next >
Encoding:
Text File  |  1999-06-13  |  7.0 KB  |  270 lines  |  [TEXT/CWIE]

  1. // Copyright (C) 1999 Eric Roccasecca.  All rights reserved.
  2.  
  3. #include "X_Ray_Priv.h"
  4.  
  5. GetNextEventFilterUPP    gOldjGNEFilter = nil, gX_RayFilterUPP = nil;
  6. WindowPtr                gCurFrontWindow = nil;
  7. NMRec                    gNMRec;
  8. UniversalProcPtr        gNotifyUPP, gGestaltUPP;
  9. Boolean                    gNMRecInUse = false;
  10. short                    gFoundWinPart;
  11. WindowPtr                gFoundWindow;
  12. FSSpec                    gInitFileSpec;
  13. long                    gIdleTime;
  14.  
  15. X_Ray_CommRec            gCommRec;
  16.  
  17. GrafPtr                    evt_origPort;
  18. RgnHandle                evt_visRgn;
  19. X_Ray_WindowHandle        evt_curWindow;
  20.  
  21.  
  22. void X_Ray_jGNE (EventRecord *theEvent, Boolean *result);
  23. void PostNoticeToUser (StringPtr notificationStr);
  24. pascal void PostNoticeToUser_CallBack ();
  25. OSErr RememberInitFile (FSSpecPtr fsp);
  26. pascal OSErr X_Ray_Gestalt (OSType selector, long *response);
  27. void IdleWindowTransparency (Point mouse);
  28. void CheckTransparentClickAppSwitch (EventRecord *theEvent);
  29.  
  30. pascal void ShowInitIcon(short iconId, Boolean advance);
  31.  
  32. void main (void)
  33. {
  34.     Handle    theCode;
  35.     THz        savedZone;
  36.     
  37.     savedZone = GetZone();
  38.     SetZone (SystemZone());
  39.     
  40.     gOldjGNEFilter = LMGetGNEFilter();
  41.     gX_RayFilterUPP = NewGetNextEventFilterProc (X_Ray_jGNE);
  42.     if (gX_RayFilterUPP)
  43.     {
  44.         theCode = Get1Resource ('INIT', 129);
  45.         DetachResource (theCode);
  46.         
  47.         gCommRec.appList = nil;
  48.         gCommRec.tsmWindowList = nil;
  49.         gCommRec.tsmLastWindow = nil;
  50.         
  51.         evt_visRgn = NewRgn();
  52.         
  53.         if (!InitInternalWindowManager())
  54.             DebugStr ("\pwindow patch failed");
  55.         
  56.         if (!InitInternalQuickDraw())
  57.             DebugStr ("\pQD patch failed");
  58.         
  59.         if (!ApplyMenuPatches())
  60.             DebugStr ("\pmenu patch failed");
  61.         
  62.         gNotifyUPP = NewNMProc (PostNoticeToUser_CallBack);
  63.         gGestaltUPP = NewSelectorFunctionProc (X_Ray_Gestalt);
  64.         NewGestalt (kX_RayGestalt, gGestaltUPP);
  65.         
  66.         LMSetGNEFilter (gX_RayFilterUPP);
  67.         
  68.         RememberInitFile (&gInitFileSpec);
  69.         
  70.         // init globals
  71.         gIdleTime = LMGetTicks() + 10;
  72.         
  73.         ShowInitIcon (128, true);
  74.     }
  75.     
  76.     SetZone (savedZone);
  77. }
  78.  
  79.  
  80. // handles all events regarding the floating windows
  81. void X_Ray_jGNE (EventRecord *theEvent, Boolean *result)
  82. {
  83.     if (theEvent->what == nullEvent)
  84.         IdleWindowTransparency (theEvent->where);
  85.     else if (theEvent->what == mouseDown)
  86.         CheckTransparentClickAppSwitch (theEvent);
  87.     else if (theEvent->what == keyDown)
  88.     {
  89.         //if (((theEvent->message&keyCodeMask)>>8) == 0x6F) // F12 key
  90.         //    DumpTransparentWindowList();
  91.     }
  92.     
  93.     if (gOldjGNEFilter)
  94.         CallGetNextEventFilterProc (gOldjGNEFilter, theEvent, result);
  95. }
  96.  
  97.  
  98. // see if click was in the content region of a transparent window in an app that was not the foreground app
  99. // if so, switch it to the front and give it the click
  100. void CheckTransparentClickAppSwitch (EventRecord *theEvent)
  101. {
  102.     Boolean                isSameApp;
  103.     ProcessSerialNumber    frontPSN;
  104.     EvQElPtr            newEvent;
  105.     
  106.     GetPort (&evt_origPort);
  107.     
  108.     GetFrontProcess (&frontPSN);
  109.     
  110.     evt_curWindow = GetNextWindowBehind (nil);
  111.     while (evt_curWindow)
  112.     {
  113.         SetPort ((*evt_curWindow)->theWindow);
  114.         CopyRgn ((*evt_curWindow)->theWindow->visRgn, evt_visRgn);
  115.         X_Ray_LocalToGlobalRgn (evt_visRgn);
  116.         
  117.         if (!((*evt_curWindow)->windowKind&kTSMWindow) && ((*evt_curWindow)->transparency == kTransparencyOpaque))
  118.         {
  119.             if (PtInRgn (theEvent->where, evt_visRgn))
  120.             {
  121.                 SameProcess (&frontPSN, &(*(*evt_curWindow)->owner)->appSerialNum, &isSameApp);
  122.                 if (!isSameApp)
  123.                 {
  124.                     SetFrontProcess (&(*(*evt_curWindow)->owner)->appSerialNum);
  125.                     PPostEvent (mouseDown, theEvent->message, &newEvent);
  126.                     if (newEvent)
  127.                     {
  128.                         newEvent->evtQWhen = theEvent->when;
  129.                         newEvent->evtQWhere = theEvent->where;
  130.                         newEvent->evtQModifiers = theEvent->modifiers;
  131.                     }
  132.                 }
  133.                 
  134.                 theEvent->what == nullEvent;
  135.                 
  136.                 break;
  137.             }
  138.         }
  139.         
  140.         evt_curWindow = GetNextWindowBehind (evt_curWindow);
  141.     }
  142.     
  143.     SetPort (evt_origPort);
  144. }
  145.  
  146.  
  147. void IdleWindowTransparency (Point mouse)
  148. {
  149.     Boolean                foundWindow = false;
  150.     unsigned short        newTransparency;
  151.     
  152.     GetPort (&evt_origPort);
  153.     
  154.     evt_curWindow = GetNextWindowBehind (nil);
  155.     while (evt_curWindow)
  156.     {
  157.         SetPort ((*evt_curWindow)->theWindow);
  158.         CopyRgn ((*evt_curWindow)->theWindow->visRgn, evt_visRgn); // make sure we only make a window opaque or transparent when mouse is in a visible part
  159.         X_Ray_LocalToGlobalRgn (evt_visRgn);
  160.         
  161.         // see if mouse is in window content area, if so should it be opaque or completely transparent
  162.         if (!foundWindow && PtInRgn (mouse, evt_visRgn))
  163.         {
  164.             foundWindow = true;
  165.             
  166.             if (isPressed(0x39)) // caps lock, make completely transparent
  167.             {
  168.                 if ((*evt_curWindow)->transparency < kTransparencyClear)
  169.                 {
  170.                     if ((kTransparencyClear - (*evt_curWindow)->transparency) < kTransparencyFadeIncrement)
  171.                         newTransparency = kTransparencyClear;
  172.                     else
  173.                         newTransparency = (*evt_curWindow)->transparency + kTransparencyFadeIncrement;
  174.                     SetWindowTransparency (newTransparency, (*evt_curWindow)->theWindow);
  175.                 }
  176.             }
  177.             else // make opaque
  178.             {
  179.                 if ((*evt_curWindow)->transparency > kTransparencyOpaque)
  180.                 {
  181.                     if (((*evt_curWindow)->transparency - kTransparencyOpaque) < kTransparencyFadeIncrement)
  182.                         newTransparency = kTransparencyOpaque;
  183.                     else
  184.                         newTransparency = (*evt_curWindow)->transparency - kTransparencyFadeIncrement;
  185.                     SetWindowTransparency (newTransparency, (*evt_curWindow)->theWindow);
  186.                 }
  187.             }
  188.         }
  189.         else // mouse isn't in window so be sure window is displaying a normal level of transparency
  190.         {
  191.             if ((*evt_curWindow)->transparency < (*evt_curWindow)->normalTransparency)
  192.             {
  193.                 if (((*evt_curWindow)->normalTransparency - (*evt_curWindow)->transparency) < kTransparencyFadeIncrement)
  194.                     newTransparency = (*evt_curWindow)->normalTransparency;
  195.                 else
  196.                     newTransparency = (*evt_curWindow)->transparency + kTransparencyFadeIncrement;
  197.                 SetWindowTransparency (newTransparency, (*evt_curWindow)->theWindow);
  198.             }
  199.             else if ((*evt_curWindow)->transparency > (*evt_curWindow)->normalTransparency)
  200.             {
  201.                 if (((*evt_curWindow)->transparency - (*evt_curWindow)->normalTransparency) < kTransparencyFadeIncrement)
  202.                     newTransparency = (*evt_curWindow)->normalTransparency;
  203.                 else
  204.                     newTransparency = (*evt_curWindow)->transparency - kTransparencyFadeIncrement;
  205.                 SetWindowTransparency (newTransparency, (*evt_curWindow)->theWindow);
  206.             }
  207.         }
  208.         
  209.         evt_curWindow = GetNextWindowBehind (evt_curWindow);
  210.     }
  211.     
  212.     SetPort (evt_origPort);
  213. }
  214.  
  215.  
  216. pascal OSErr X_Ray_Gestalt (OSType selector, long *response)
  217. {
  218.     *response = (long)&gCommRec;
  219.     return noErr;
  220. }
  221.  
  222.  
  223. void PostNoticeToUser (StringPtr notificationStr)
  224. {
  225.     OSErr    err;
  226.     
  227.     if (!gNMRecInUse)
  228.     {
  229.         gNMRec.qType = 8;
  230.         gNMRec.nmMark = 0;
  231.         gNMRec.nmIcon = nil;
  232.         gNMRec.nmSound = (Handle)-1;
  233.         gNMRec.nmStr = notificationStr;
  234.         gNMRec.nmResp = gNotifyUPP;
  235.         
  236.         gNMRecInUse = true;
  237.         err = NMInstall (&gNMRec);
  238.     }
  239.     else
  240.         SysBeep (1);
  241. }
  242.  
  243.  
  244. pascal void PostNoticeToUser_CallBack (NMRecPtr theNMRec)
  245. {
  246.     gNMRecInUse = false;
  247.     NMRemove (theNMRec);
  248. }
  249.  
  250.  
  251. // Remembers the location of our file
  252. OSErr RememberInitFile (FSSpecPtr fsp)
  253. {
  254.     FCBPBRec        pb;
  255.     OSErr           err = noErr;
  256.     
  257.     pb.ioCompletion = nil;
  258.     pb.ioNamePtr = fsp->name;
  259.     pb.ioVRefNum = 0;
  260.     pb.ioRefNum = CurResFile();
  261.     pb.ioFCBIndx = 0;
  262.     
  263.     err = PBGetFCBInfoSync (&pb);
  264.  
  265.     fsp->vRefNum = pb.ioFCBVRefNum;
  266.     fsp->parID = pb.ioFCBParID;
  267.     
  268.     return err;
  269. }
  270.